STATIC Statement ---------------------------------------------------------------------------- Action Makes simple variables or arrays local to a DEF FN function, a FUNCTION procedure, or a SUB procedure, and preserves values between calls. Syntax STATIC variable( ) AS type , variable( ) AS type... Remarks The STATIC statement uses the following arguments. ----------------------------------------------------------------------------- Argument Description ---------------------------------------------------------------------------- variable The variable the procedure will use. It is either an array name followed by ( ), or a variable name. AS type Declares the type of the variable. The type can be INTEGER, LONG, SINGLE, DOUBLE, STRING (for variable-length strings), STRING * length (for fixed-length strings), CURRENCY, or a user-defined type. Argument Description ---------------------------------------------------------------------------- STATIC is a BASIC declaration that makes simple variables or arrays local to a procedure (or a function defined by DEF FN) and preserves the variable value between procedure calls. The STATIC statement can appear only in a SUB or FUNCTION procedure or DEF FN function. Earlier versions of BASIC required the number of dimensions in parentheses after an array name. The number of dimensions in BASIC is now optional. Variables declared in a STATIC statement override variables of the same name shared by DIM or COMMON statements in the module-level code. Variables in a STATIC statement also override global constants of the same name. Usually, variables used in DEF FN functions are global to the module; however, you can use the STATIC statement inside a DEF FN statement to declare a variable as local to only that function. The differences between the STATIC statement, the STATIC attribute, and the $STATIC metacommand are. ----------------------------------------------------------------------------- Statement-Attribute Differences ---------------------------------------------------------------------------- STATIC attribute on SUB and Declares default for variables to be FUNCTION statements static. Variables having the same name as variables shared by module-level code are still shared. STATIC statement Makes specific variables static and overrides any variables shared by the module-level code. $STATIC metacommand Affects how memory is allocated for arrays. See Also DEF FN, FUNCTION, SUB Example The following example searches for every occurrence of a certain string expression in a file and replaces that string with another string. The program also prints the number of substitutions and the number of lines changed. INPUT "Name of file";F1$ INPUT "String to replace";Old$ INPUT "Replace with";Nw$ Rep = 0 . Num = 0 M = LEN(Old$) OPEN F1$ FOR INPUT AS #1 CALL Extension OPEN F2$ FOR OUTPUT AS #2 DO WHILE NOT EOF(1) LINE INPUT #1, Temp$ CALL Search PRINT #2, Temp$ LOOP CLOSE PRINT "There were ";Rep;" substitutions in ";Num;" lines." PRINT "Substitutions are in file ";F2$ END SUB Extension STATIC SHARED F1$,F2$ Mark = INSTR(F1$,".") IF Mark = 0 THEN F2$ = F1$ + ".NEW" ELSE F2$ = LEFT$(F1$,Mark - 1) + ".NEW" END IF END SUB SUB Search STATIC SHARED Temp$,Old$,Nw$,Rep,Num,M STATIC R Mark = INSTR(Temp$,Old$) WHILE Mark Part1$ = LEFT$(Temp$,Mark - 1) Part2$ = MID$(Temp$,Mark + M) Temp$ = Part1$ + Nw$ + Part2$ R = R + 1 Mark = INSTR(Temp$,Old$) WEND IF Rep = R THEN EXIT SUB ELSE Rep = R Num = Num + 1 END IF END SUB